package org.lboot.flow.event;

import org.lboot.flow.module.event.FlowCustomEvent;
import org.springframework.scheduling.annotation.Async;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author kindear
 * 流程内部事件消费者
 */
public interface FlowEventConsumer {
    // 消费的事件ID
    String eventId();

    // 消费者ID
    String consumerId();

    // 消费者名称
    String consumerName();

    // 优先级 --> 系统默认优先级
    default Integer priority(){
        return 100;
    }

    // 事件同步消费执行
    Map<String,Object> syncConsume(FlowCustomEvent event);

    // 事件异步消费
    @Async
    void asyncConsume(FlowCustomEvent event);

    default Map<String, Object> consume(FlowCustomEvent event){
        if (event.getIsAsync()){
            // 异步执行
            CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
                asyncConsume(event);
            });

        }else {
            return syncConsume(event);
        }
        return null;
    }
    // 消费者基础信息
    default FlowEventConsumerMeta meta(){
        return new FlowEventConsumerMeta();
    }
}
